<?php
namespace app\admin\controller;
use app\model\Admin as AdminModel;
use think\Db;

use think\captcha\Captcha;


class Admin extends Base
{
    public $exception = ['login','getcaptcha'];

    public function __construct(){
        $model = new AdminModel();
        $this->model = $model;
        parent::__construct();

        $page_info = $this->page_info;
        $page_info['title'] = '管理员';

        if ( in_array($page_info['action'], ['add','edit']) ) {
            $fields = [
                'account'=>['name'=>'用户名','type'=>'text','valids'=>['required']],
                'password'=>['name'=>'密码','type'=>'text','valids'=>['required']],
                'role'=>['name'=>'角色权限','type'=>'select1','params'=>$model->getRoleList()],
                'truename'=>['name'=>'姓名','type'=>'text'],
                'mobile'=>['name'=>'手机号码','type'=>'text']
            ];
            if ($page_info['action'] == 'edit') {
                $fields['password']['placeholder'] = '留空，则不修改';
                $fields['password']['value'] = '';
                unset($fields['password']['valids']);
            }
            
            $valids = createPageValid($fields);
            $this->assign([
                'fields'=>$fields,
                'valids'=>$valids
            ]);
        }
        
        $this->assign([
            'model'=>$model,
            'page_info'=>$page_info
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'account','type'=>'like'],
            ['key'=>'truename','type'=>'like'],
            ['key'=>'role','type'=>'=']
        ];
        $map = getSearchParam($map_params);

        $search_params = [
            [
                ['key'=>'account','name'=>'用户名','type'=>'text'],
                ['key'=>'truename','name'=>'姓名','type'=>'text'],
                ['key'=>'role','name'=>'角色','type'=>'select1_1','params'=>$model->getRoleList()],
                ['key'=>'start_time','name'=>'开始时间','type'=>'datetime'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);

        return parent::index($map);
    }

    public function getCaptcha(){
        $captcha = new Captcha([        
            'codeSet'  => '1234567890', 
            'fontSize' => 30,
            'useCurve' => false,
            'imageH'   => 90,
            'length'   => 4,
            'reset'    => true
        ]);
        $data = $captcha->entry();
        return $data->getData();
    }

    public function login(){
        if (request()->isPost()) {
            $model = $this->model;
            $param = request()->param();

            if( !captcha_check($param['verify']) ){
                $this->error("验证码错误");
            }

            $data = $model->where('account',$param['account'])->find();
            if ( $data['password'] != md5($param['password']) ) {
                $this->error("账号或密码错误");
            }else{
                // 更新登录时间
                $model->where('id',$data['id'])->update(['login_time'=>time()]);

                session('cch_admin',$data);
                return redirect(url('index/index'));
            }
        }
        return view('login', [
            
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['account','password','role'],
                'nomust'=>['truename','mobile']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            $save_data['password'] = md5($save_data['password']);
        }
        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        if (request()->isPost()) {
            $fields = [
                'nomust'=>['account','password','role','truename','mobile']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['password'])) {
                $save_data['password'] = md5($save_data['password']);
            }else{
                unset($save_data['password']);
            }
        }
        return parent::updateData($id,$save_data);
    }


    public function logout(){
        session('cch_admin',null);
        return redirect(url('login'));
    }
   
}
